package com.suppor.rpc.client.proxy;


import com.suppor.rpc.register.RegisterService;
import com.suppor.rpc.register.ZKRegisterCenter;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

public class ClientBean implements FactoryBean<Object> {
    private Class<?> serviceClass;
    private String serverAddr;
    private int serverPort;
    private int timeOut;
    private String registerType;
    private String registerAdd;
    private String serviceVersion;
    private Object proxy;


    @Override
    public Object getObject() throws Exception {
        return proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return serviceClass;
    }

    // FactoryBean 初始化时可以用init方法完成自定义Bean的初始化
    public void init() throws Exception {
        RegisterService register = new ZKRegisterCenter(this.registerAdd);
        this.proxy = Proxy.newProxyInstance(serviceClass.getClassLoader(),
                new Class<?>[]{serviceClass},
                new ClientInvocationHandler(serviceVersion, timeOut, register));
    }

    public Class<?> getServiceClass() {
        return serviceClass;
    }

    public void setServiceClass(Class<?> serviceClass) {
        this.serviceClass = serviceClass;
    }

    public String getServerAddr() {
        return serverAddr;
    }

    public void setServerAddr(String serverAddr) {
        this.serverAddr = serverAddr;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }

    public String getRegisterType() {
        return registerType;
    }

    public void setRegisterType(String registerType) {
        this.registerType = registerType;
    }

    public String getRegisterAdd() {
        return registerAdd;
    }

    public void setRegisterAdd(String registerAdd) {
        this.registerAdd = registerAdd;
    }

    public Object getProxy() {
        return proxy;
    }

    public void setProxy(Object proxy) {
        this.proxy = proxy;
    }

    public int getTimeOut() {
        return timeOut;
    }

    public void setTimeOut(int timeOut) {
        this.timeOut = timeOut;
    }

    public String getServiceVersion() {
        return serviceVersion;
    }

    public void setServiceVersion(String serviceVersion) {
        this.serviceVersion = serviceVersion;
    }
}
